package com.reggie.module.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.reggie.common.ResultResponseVo;
import com.reggie.module.entity.po.AddressBook;
import com.reggie.module.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 地址簿管理
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增
     */
    @PostMapping
    public ResultResponseVo save(@RequestBody AddressBook addressBook, HttpServletRequest request) {
        return addressBookService.saveAddress(addressBook,request);
    }

    /**
     * 更新地址
     * @param addressBook
     * @param request
     * @return
     */
    @PutMapping
    public ResultResponseVo updateById(@RequestBody AddressBook addressBook,HttpServletRequest request){
        return addressBookService.updateAddress(addressBook,request);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/default")
    public ResultResponseVo setDefault(@RequestParam(value = "id")Long id, HttpServletRequest request) {
        return addressBookService.setDefault(id,request);
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public ResultResponseVo get(@PathVariable Long id) {
        return addressBookService.getAddressById(id);
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public ResultResponseVo getDefault(HttpServletRequest request) {
        return addressBookService.getDefaultAddress(request);
    }

    /**
     * 查询指定用户的全部地址
     */
    @GetMapping("/list")
    public ResultResponseVo list(HttpServletRequest request) {
        return addressBookService.getListByUserId(request);
    }
}
